<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<link rel="stylesheet" href="./layui/css/layui.css" media="all">
</head>
<body>
<table id="demo" class="layui-table"></table>
<button onclick="tabletest()">表格</button>
<button onclick="start()">测试</button>
<button onclick="change_url()">改变url</button>
<button onclick="query_alarm()">查询报警</button>
<button onclick="test_time()">测试时间</button>
<div id="messages"></div>

<script src="./layui/layui.js"></script>

<script type="text/javascript">
	var table;

	var tableData1= {id:"3", name:"khkhj"};
	var tableData2= '{"code": 0,"msg": "","count": 1,"data": [{"id":"6", "name":"fsadf"}]}';

	layui.use(['table'], function(){
		table = layui.table;

		table.render({
		  elem: '#demo',
			data: tableData1,
			page: false,
			
			cols: [[
					{field:'id', title: 'id', sort: true},
					{field:'name',  title: 'name',edit:'text'}
			]],
			data:[
				{id:"1", "name":"sdf"},
				{id:"2", "name":"fghfgh"}
			]
		});
	});
	
	function test_time(){
		var date = new Date("0001-01-01T00:00:00");
		var t = date.Format("yyyy-MM-dd hh:mm:ss");
	}

	var webSocket = null;

	function change_url(){
		webSocket.close();
		webSocket = new WebSocket('ws://172.16.168.88/websocket/xy');	
		url = webSocket.url;
		
		webSocket.addEventListener('open', function () {
			webSocket.send('asd');
		});
	}
	
	function tabletest(){
		var dataBak = [];
		// var tableBak = table.cache.demo; 
		var cache = table.cache["demo"];
		cache.push(tableData1);

		table.reload('demo', {
			data:cache
		});
	}

	function query_alarm(){
		webSocket.send("{\r\n    \"Query\":\"HistoryAlarm\", \r\n    \"StationName\":\"上安站\", \r\n    \"AlarmType\":-1, \r\n    \"AlarmLevel\":-1, \r\n    \"BeginTime\":\"2019-04-17 00:00:00\", \r\n    \"EndTime\":\"2019-04-17 23:59:59\", \r\n    \"Result\":\"\"   \r\n}");
	}

	function init(){
		webSocket = new WebSocket('ws://172.16.168.88/websocket/xy');	

		webSocket.onerror = function(event) 
		{
		  onError(event)
		};
		
		webSocket.onopen = function(event) 
		{
		  onOpen(event)
		};
		
		webSocket.onmessage = function(event)
		{
		  onMessage(event)
		};

		webSocket.onclose = function(event)
		{
		  onClose(event)
		};
	}
	
	function onMessage(event)
	{
	  	document.getElementById('messages').innerHTML += '<br />' + event.data;
	}
	
	function onOpen(event) 
	{
	  	document.getElementById('messages').innerHTML  = 'Connection established';
	}

	function onClose(event) 
	{
	  	document.getElementById('messages').innerHTML  = 'Connection closed';
	}
	
	function onError(event) 
	{
	  	alert(event.data);
	}
	
	function start() 
	{
		init();
	}
</script>
</body>
</html>